package com.info.zhiduoduo.core.service.common;

import com.alibaba.excel.util.FileUtils;
import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.configs.DocConfig;
import com.info.zhiduoduo.common.configs.UploadPathConfig;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.crowdsource.CrowdsourceUserType;
import com.info.zhiduoduo.common.exception.Asserts;
import com.info.zhiduoduo.common.exception.SmartException;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.FileUtil;
import com.info.zhiduoduo.common.utils.ImgBase64Util;
import com.info.zhiduoduo.common.utils.WordUtil;
import com.info.zhiduoduo.core.formbean.SealFormBean;
import com.info.zhiduoduo.repository.dto.company.CompanySignContractDto;
import com.info.zhiduoduo.repository.dto.crowdsource.CrowdsourceContractDto;
import com.info.zhiduoduo.repository.dto.entrustmentproject.ProjectEntrusteeInfoDto;
import com.info.zhiduoduo.repository.dto.project.LongProjectDto;
import com.info.zhiduoduo.repository.dto.project.ProjectContractDto;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProject;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProjectItem;
import com.info.zhiduoduo.repository.entity.entrustmentproject.EntrustmentProject;
import com.info.zhiduoduo.repository.entity.system.Address;
import com.info.zhiduoduo.repository.mapper.company.CompanyProjectMapper;
import com.info.zhiduoduo.repository.mapper.company.LongTermProjectMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceProjectMapper;
import com.info.zhiduoduo.repository.mapper.entrustmentproject.EntrustmentProjectItemMapper;
import com.info.zhiduoduo.repository.mapper.system.AddressMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInvoiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 生成合同服务
 * @author tianyonghong
 * @create 2022/2/25
 */
@Slf4j
@Service
public class ContractDocService extends BaseService {
    @Autowired
    DocConfig docConfig;

    @Resource
    UploadPathConfig uploadPathConfig;
    @Resource
    TaxerInvoiceMapper taxerInvoiceMapper;
    @Resource
    CompanyProjectMapper companyProjectMapper;
    @Resource
    LongTermProjectMapper longTermProjectMapper;
    @Resource
    CrowdsourceProjectMapper crowdsourceProjectMapper;
    @Resource
    EntrustmentProjectItemMapper entrustmentProjectItemMapper;
    @Resource
    SealService sealService;
    @Autowired
    AddressMapper addressMapper;

    /**
     * 众包合同文件默认路径
     *
     * @param projectId
     * @return
     */
    private String generateCrowdsourceContractFilePath(Integer projectId) {
        return StringUtils.joinWith(File.separator, docConfig.getCrowdsourceDocFilePath(), projectId);
    }
    /**
     * 委托接单合同默认存放路径
     * @return
     */
    private String generateEntrustContractFolder(Integer projectId) {
        return StringUtils.joinWith(File.separator, docConfig.getEntrusteeDocFilePath(), projectId);
    }
    /**
     * 长期项目合同默认存放路径
     * @return
     */
    private String generateLongTermContractFolder(Integer projectId) {
        return StringUtils.joinWith(File.separator, docConfig.getLongTermDocFilePath(), projectId);
    }

    /**
     *  合同默认存放路径
     * @return
     */
    private String generateTaxerContractFolder(Integer projectId) {
        return StringUtils.joinWith(File.separator, docConfig.getDocFilePath(), projectId);
    }

    /**
     * 批量下载 项目合同
     *
     * @param response
     * @param projectIdStr
     */
    public void batchDownloadContractPdfZip(HttpServletResponse response, String projectIdStr) {
        String[] projectIds = projectIdStr.split(",");

        // 合同存放临时路径
        String tmpFolder = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis();
        File temDestDir = new File(tmpFolder);

        try {
            // 生成合同
            for (String projectId : projectIds) {
                int projectid = Integer.parseInt(projectId);
                File contractDocFile = createContractFile(projectid);
                org.apache.commons.io.FileUtils.copyFileToDirectory(contractDocFile, temDestDir);
            }

            // 生成压缩包 压缩包名称
            String zipName = " 项目合同_" + DateUtil.getTodayStr() + ".zip";
            // 调用方法打包zip文件
            FileUtil.createZip2Response(response, tmpFolder, zipName);
        } catch (Exception e) {
            log.error("将 合同文件压缩为压缩包失败！", e);
        } finally {
            // 删除临时文件夹
            FileUtil.deleteDir(tmpFolder);
        }
    }

    /**
     * 下载 项目合同
     *
     * @param response
     * @param projectid
     */
    public void downloadContract(HttpServletResponse response, int projectid) {

        File contractDocFile = createContractFile(projectid);
        //下载
        try {
            FileUtil.downloadFile(response, contractDocFile);
        } catch (Exception e) {
            log.error("下载 合同文件异常，error:{}", e);
        }
    }

    /**
     * 创建 项目合同文件
     * @param projectid
     * @return
     */
    public File createContractFile(int projectid) {
        //校验数据是否存在
        ProjectContractDto contractDto = companyProjectMapper.getProjectContractInfo(projectid);
        Asserts.notNull(contractDto, StatusMsg.StatusMsg_301);
        Asserts.notNull(contractDto.getUsername(), StatusMsg.StatusMsg_201);
        //合同信息
        contractDto.setContractDocName(contractDto.getUsername() + "_" + contractDto.getCompname() + "_" + contractDto.getProjectid() + "_" + contractDto.getCreatetime().getTime() + ".pdf");
        contractDto.setFileFolderPath(docConfig.getDocFilePath());

        //生成文件
        if (contractDto.getCategoryid() == 34) {
            // 商品交易，使用商品交易合同
            return generateContractPdfFile(contractDto, docConfig.getCommodityContractTemplateName());
        } else {
            return generateContractPdfFile(contractDto, docConfig.getContractTemplateName());
        }
    }

    /**
     * 生成 / 商品合同/ 发票项目合同文件
     *
     * @param contractDto
     * @param templateName
     * @return
     */
    private File generateContractPdfFile(ProjectContractDto contractDto, String templateName) {

        File filePath = new File(contractDto.getFileFolderPath(), DateUtil.getString(contractDto.getContractDate(), DateUtil.FORMAT_STYLE_5));
        if (!filePath.exists()) {
            filePath.mkdirs();
        }

        File contractDocFile = new File(filePath, contractDto.getContractDocName());
        if (!contractDocFile.exists()) {
            FileUtils.delete(contractDocFile);

            //生成公章
            SealFormBean sealFormBean = sealService.createCompOrUserSeal(contractDto.getCompname(), contractDto.getUsername());

            String usernameAutograph = getElectronicSign(contractDto);
            Map<String, Object> data = ProjectContractDto.ofMap(contractDto);
            data.put("usernameAutograph", StringUtils.isBlank(usernameAutograph) ? "" : ImgBase64Util.ImgToBase64(usernameAutograph));
            data.put("compseal", ImgBase64Util.ImgToBase64(sealFormBean.getCompsealFile().getAbsolutePath()));
            data.put("userseal", ImgBase64Util.ImgToBase64(sealFormBean.getUsersealFile().getAbsolutePath()));
            //获取商品明细文件-图片
            String commodityPath = getCommodityFile(contractDto.getCommodityDetailFilePath());
            data.put("commodityAttachment", StringUtils.isBlank(commodityPath) ? "" : ImgBase64Util.ImgToBase64(commodityPath));

            try {
                //生成合同
                WordUtil.createHtmlToPdf(data, templateName, filePath.getAbsolutePath(), contractDto.getContractDocName());
            } catch (Exception e) {
                log.error("合同名称：{},生成合同报错：", contractDto.getContractDocName(), e);
                throw SmartException.of(StatusMsg.StatusMsg_199);
            }
        }
        return contractDocFile;
    }

    /**
     * 下载众包子项目合同文件
     *
     * @param response
     * @param projectItemId
     */
    public void downloadCrowdSourceItemContractPdf(HttpServletResponse response, int projectItemId) {
        // 获取合同数据
        CrowdsourceContractDto contractDto = crowdsourceProjectMapper.getProjectContractInfo(projectItemId);
        Asserts.notNull(contractDto, StatusMsg.StatusMsg_610);
        Asserts.notNull(contractDto.getCompname(), StatusMsg.StatusMsg_301);

        // 合同路径
        String fileFolderPath = generateCrowdsourceContractFilePath(contractDto.getProjectid());
        // 生成合同文件
        File contractFile = generateCrowdsourceContractPdfFile(contractDto,fileFolderPath);

        // 下载
        try {
            FileUtil.downloadFile(response, contractFile);
        } catch (Exception e) {
            log.error("下载众包子项目合同文件异常，error:{}", e);
        }
    }

    /**
     * 下载众包项目合同包含所有子项目的zip文件包
     *
     * @param response  response
     * @param projectid projectid
     */
    public void downloadCrowdSourceContractZip(HttpServletResponse response, int projectid) {
        // 获取项目信息
        CrowdsourceProject crowdsourceProject = new CrowdsourceProject().selectById(projectid);
        Asserts.notNull(crowdsourceProject, StatusMsg.StatusMsg_301);
        // 子项目列表
        List<CrowdsourceProjectItem> itemList = new CrowdsourceProjectItem().selectList(Condition.create().eq("projectid", projectid).eq("deleted", 0));
        Asserts.notEmpty(itemList, StatusMsg.StatusMsg_610);

        // 合同存放路径
        String fileFolderPath = generateCrowdsourceContractFilePath(projectid);
        //生成所有文件，添加到List
        List<File> contractList = Lists.newArrayList();
        for (CrowdsourceProjectItem item : itemList) {
            CrowdsourceContractDto contractDto = crowdsourceProjectMapper.getProjectContractInfo(item.getId());
            if (contractDto.getWorkType() == 1) {
                contractList.add(generateCrowdsourceContractPdfFile(contractDto, fileFolderPath));
            }else {
                contractList.add(generateOddJobContractPdfFile(contractDto, fileFolderPath));
            }
        }

        // 生成压缩包
        Asserts.notEmpty(contractList, StatusMsg.StatusMsg_650);
        try {
            // 压缩包名称
            String zipName = crowdsourceProject.getCompname() + "-" +
                    DateUtil.DateToStr("yyyy-MM-dd", crowdsourceProject.getFinishtime()) + "-" +
                    crowdsourceProject.getProjectName() + ".zip";
            // 调用方法打包zip文件
            FileUtil.createZip2Response(response, fileFolderPath, zipName);
        } catch (Exception e) {
            log.error("将众包项目文件夹压缩为压缩包失败！", e);
        }
    }


    /**
     * 生成智众包子项目合同
     *
     * @return
     */
    private File generateCrowdsourceContractPdfFile(CrowdsourceContractDto contractDto,String fileFolderPath) {
        File filePath = new File(fileFolderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //合同名称
        String contractDocName = contractDto.getStudioName() + "-" + contractDto.getUserid() + "-" + contractDto.getId() + ".pdf";
        File contractDocFile = new File(filePath, contractDocName);
        if (!contractDocFile.exists()) {
            FileUtils.delete(contractDocFile);
            //生成公章
            String username = contractDto.getUserType() == CrowdsourceUserType.NATURAL_PERSON ? null : contractDto.getStudioName();
            SealFormBean sealFormBean = sealService.createCompOrUserSeal(contractDto.getPartyName(), username);

            Map<String, Object> data = CrowdsourceContractDto.ofMap(contractDto);
            data.put("compseal", ImgBase64Util.ImgToBase64(sealFormBean.getCompsealFile().getAbsolutePath()));
            data.put("userseal", sealFormBean.getUsersealFile() == null ? "" : ImgBase64Util.ImgToBase64(sealFormBean.getUsersealFile().getAbsolutePath()));

            try {
                //生成合同
                WordUtil.createHtmlToPdf(data, docConfig.getCrowdsourceItemContractTemplateName(), filePath.getAbsolutePath(), contractDocName);
            } catch (Exception e) {
                log.error("众包子项目合同名称：{},生成合同报错：", contractDocName, e);
                throw SmartException.of(StatusMsg.StatusMsg_199);
            }
        }
        return contractDocFile;
    }
    /**
     * 生成智零工子项目合同
     *
     * @return
     */
    private File generateOddJobContractPdfFile(CrowdsourceContractDto contractDto,String fileFolderPath) {
        File filePath = new File(fileFolderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //合同名称
        String contractDocName = contractDto.getStudioName() + "-" + contractDto.getUserid() + "-" + contractDto.getId() + ".pdf";
        File contractDocFile = new File(filePath, contractDocName);
        if (!contractDocFile.exists()) {
            FileUtils.delete(contractDocFile);
            //生成公章
            SealFormBean sealFormBean = sealService.createCompOrUserSeal(contractDto.getPartyName(), null);

            Map<String, Object> data = CrowdsourceContractDto.ofMap(contractDto);
            data.put("compseal", ImgBase64Util.ImgToBase64(sealFormBean.getCompsealFile().getAbsolutePath()));
            String Province = "";
            String City = "";
            Address address = addressMapper.getAddress(contractDto.getProvince());
            if (null != address && StringUtils.isNotBlank(address.getName())){
                Province = address.getName();
            }
            Address address1 = addressMapper.getAddress(contractDto.getCity());
            if (null != address1 && StringUtils.isNotBlank(address1.getName())){
                City = address1.getName();
            }
            data.put("address", Province+City);
            try {
                //生成合同
                WordUtil.createHtmlToPdf(data, docConfig.getOddJobItemContractTemplateName(), filePath.getAbsolutePath(), contractDocName);
            } catch (Exception e) {
                log.error("众包子项目合同名称：{},生成合同报错：", contractDocName, e);
                throw SmartException.of(StatusMsg.StatusMsg_199);
            }
        }
        return contractDocFile;
    }

    /**
     * 批量下载委托接单合同zip文件
     * @param response
     * @param projectIdStr
     */
    public void batchDownloadEntrustContractPdfZip(HttpServletResponse response, String projectIdStr){
        String[] projectIds = projectIdStr.split(",");

        // 对每个子项目生成合同文件，并存入文件夹
        List<File> contractList = Lists.newArrayList();

        // 合同存放临时路径
        String tmpFolder = System.getProperty("java.io.tmpdir") + File.separator
                + "entCon" + System.currentTimeMillis();

        for (String projectId:projectIds) {
            Integer projectid = Integer.parseInt(projectId);

            EntrustmentProject entrustmentProject = new EntrustmentProject().selectById(projectid);
            if (entrustmentProject == null) {
                continue;
            }

            List<ProjectEntrusteeInfoDto> infoList = entrustmentProjectItemMapper.getEntrustProjectContractInfo(projectid, null);
            if (CollectionUtils.isEmpty(infoList)) {
                continue;
            }

            String tmpFolderProject = tmpFolder + File.separator + projectId;
            for (ProjectEntrusteeInfoDto dto : infoList) {
                contractList.add(generateEntrustContractPdfFile(dto, tmpFolderProject));
            }
        }

        // 生成压缩包
        Asserts.notEmpty(contractList, StatusMsg.StatusMsg_650);
        try {
            // 压缩包名称
            String zipName = "合同_" + DateUtil.getTodayStr() + ".zip";
            // 调用方法打包zip文件
            FileUtil.createZip2Response(response, tmpFolder, zipName);
        } catch (Exception e) {
            log.error("将委托接单合同文件压缩为压缩包失败！", e);
        } finally {
            // 删除临时文件夹
            FileUtil.deleteDir(tmpFolder);
        }
    }

    /**
     * 下载委托接单合同zip文件
     * @param response
     * @param projectid
     */
    public void downloadEntrustContractZip(HttpServletResponse response, int projectid) {
        EntrustmentProject entrustmentProject = new EntrustmentProject().selectById(projectid);
        Asserts.notNull(entrustmentProject, StatusMsg.StatusMSG_1101);

        List<ProjectEntrusteeInfoDto> infoList = entrustmentProjectItemMapper.getEntrustProjectContractInfo(projectid, null);
        Asserts.notEmpty(infoList, StatusMsg.StatusMSG_1101);

        // 合同存放路径
        String fileFolderPath = generateEntrustContractFolder(projectid);
        // 对每个子项目生成合同文件，并存入文件夹
        List<File> contractList = Lists.newArrayList();
        for (ProjectEntrusteeInfoDto dto : infoList) {
            contractList.add(generateEntrustContractPdfFile(dto, fileFolderPath));
        }

        // 生成压缩包
        Asserts.notEmpty(contractList, StatusMsg.StatusMsg_650);
        try {
            // 压缩包名称
            String zipName = entrustmentProject.getCompname() + "-" + DateUtil.DateToStr("yyyy-MM-dd", entrustmentProject.getCreatetime()) + "-" +
                    entrustmentProject.getOriginFileName() + ".zip";
            // 调用方法打包zip文件
            FileUtil.createZip2Response(response, fileFolderPath, zipName);
        } catch (Exception e) {
            log.error("将委托接单合同文件压缩为压缩包失败！", e);
        }
    }

    /**
     * 下载委托接单合同
     *
     * @param response
     * @param projectid
     * @param entrusteeId
     */
    public void downloadEntrustContract(HttpServletResponse response, int projectid, int entrusteeId) {
        EntrustmentProject entrustmentProject = new EntrustmentProject().selectById(projectid);
        Asserts.notNull(entrustmentProject, StatusMsg.StatusMSG_1101);

        List<ProjectEntrusteeInfoDto> infoList = entrustmentProjectItemMapper.getEntrustProjectContractInfo(projectid, entrusteeId);
        Asserts.notEmpty(infoList, StatusMsg.StatusMsg_1110);
        ProjectEntrusteeInfoDto contractDto = infoList.get(0);
        //合同路径
        String fileFolderPath = generateEntrustContractFolder(contractDto.getImportId());
        File contractDocFile = generateEntrustContractPdfFile(contractDto, fileFolderPath);
        //下载
        try {
            FileUtil.downloadFile(response, contractDocFile);
        } catch (Exception e) {
            log.error("下载委托接单合同文件异常，error:{}", e);
        }
    }

    /**
     * 生成委托接单项目合同(同众包项目合同模版)
     *
     * @return
     */
    private File generateEntrustContractPdfFile(ProjectEntrusteeInfoDto contractDto,String fileFolderPath) {
        File filePath = new File(fileFolderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //合同名称
        String contractDocName = contractDto.getEntrusteeStudio() + "-" + contractDto.getEntrusteeId() + ".pdf";
        File contractDocFile = new File(filePath, contractDocName);
        if (!contractDocFile.exists()) {
            FileUtils.delete(contractDocFile);
            //生成公章
            SealFormBean sealFormBean = sealService.createCompOrUserSeal(contractDto.getCompname(), contractDto.getEntrusteeStudio());

            Map<String, Object> data = ProjectEntrusteeInfoDto.ofMap(contractDto);
            data.put("compseal", ImgBase64Util.ImgToBase64(sealFormBean.getCompsealFile().getAbsolutePath()));
            data.put("userseal", ImgBase64Util.ImgToBase64(sealFormBean.getUsersealFile().getAbsolutePath()));

            try {
                //生成合同
                WordUtil.createHtmlToPdf(data, docConfig.getCrowdsourceItemContractTemplateName(), filePath.getAbsolutePath(), contractDocName);
            } catch (Exception e) {
                log.error("委托接单项目合同名称：{},生成合同报错：", contractDocName, e);
                throw SmartException.of(StatusMsg.StatusMsg_199);
            }
        }
        return contractDocFile;
    }

    /**
     * 生成 发票项目合同
     *
     * @param invoiceId
     * @return
     */
    public File generateInvoiceContractPdfFile(Integer invoiceId) {
        //校验数据是否存在
        ProjectContractDto contractDto = taxerInvoiceMapper.getProjectContractInfo(invoiceId);
        Asserts.notNull(contractDto, StatusMsg.StatusMsg_301);
        Asserts.notNull(contractDto.getUsername(), StatusMsg.StatusMsg_201);
        //合同信息
        contractDto.setContractDocName(contractDto.getUsername()+ "_" + contractDto.getProjectname() + "_INVOICE_" + invoiceId + ".pdf");
        contractDto.setFileFolderPath(docConfig.getDocFilePath());

        //生成文件
        return generateContractPdfFile(contractDto, docConfig.getContractTemplateName());
    }

    /**
     * 是否临时登录，获取电子签名图片存储路径
     *
     * @param contractDto
     * @return
     */
    public String getElectronicSign(ProjectContractDto contractDto) {
        if (contractDto.getTemporaryLogin() == 1 && StringUtils.isNotBlank(contractDto.getElectronicSignature())) {
            return uploadPathConfig.getUploadpath() + File.separator + contractDto.getElectronicSignature();
        }
        return "";
    }

    /**
     * 获取商品明细图片文件路径，不是图片返回空
     *
     * @param commodityPath
     * @return
     */
    public String getCommodityFile(String commodityPath) {
        if (StringUtils.isBlank(commodityPath)) {
            return null;
        }
        String wholePath = uploadPathConfig.getUploadpath() + File.separator + commodityPath;
        try {
            File commodityFile = new File(wholePath);
            Image image = ImageIO.read(commodityFile);
            if (image != null) {
                return wholePath;
            }
        } catch (Exception e) {
            log.error("商品明细文件错误：" + wholePath);
        }
        return null;
    }

    /**
     * 下载长期项目多个子项目合同压缩文件
     *
     * @param response
     * @param parentProjectId
     */
    public void downloadLongProjectContractPdfZip(HttpServletResponse response, Integer parentProjectId) {

        LongProjectDto longProjectDto = longTermProjectMapper.getLongProjectDetail(parentProjectId);
        Asserts.notNull(longProjectDto, StatusMsg.StatusMsg_301);

        // 获取合同数据
        List<ProjectContractDto> contractDtoList = companyProjectMapper.getProjectContractInfoList(parentProjectId);
        Asserts.notNull(contractDtoList, StatusMsg.StatusMsg_301);

        // 合同存放路径
        String fileFolderPath = generateLongTermContractFolder(parentProjectId);
        //生成所有文件，添加到List
        List<File> contractList = Lists.newArrayList();
        for (ProjectContractDto contractDto : contractDtoList) {
            //合同信息
            contractDto.setContractDocName(contractDto.getUsername() + "_" + contractDto.getProjectname() + "_" + contractDto.getProjectid() + "_" + contractDto.getCreatetime().getTime() + ".pdf");
            contractDto.setFileFolderPath(fileFolderPath);
            //生成文件
            if (contractDto.getCategoryid() == 34) {
                // 商品交易，使用商品交易合同
                contractList.add(generateContractPdfFile(contractDto, docConfig.getCommodityContractTemplateName()));
            } else {
                contractList.add(generateContractPdfFile(contractDto, docConfig.getContractTemplateName()));
            }
        }
        // 生成压缩包
        Asserts.notEmpty(contractList, StatusMsg.StatusMsg_301);

        try {
            // 压缩包名称
            String zipName = longProjectDto.getCompname() + "-" +
                    DateUtil.DateToStr("yyyy-MM-dd", new Date()) + "-" +
                    longProjectDto.getProjectname() + ".zip";
            // 调用方法打包zip文件
            FileUtil.createZip2Response(response, fileFolderPath, zipName);
        } catch (Exception e) {
            log.error("将长期项目文件夹压缩为压缩包失败！", e);
        }
    }

    /**
     * 企业在线签署合同
     * @return 返回文件路径
     */
    public String companySignContract(CompanySignContractDto dto) {
        // 该方法返回的合同路径
        String resultPath = "confirm/onlineSign/" + DateUtil.getTodayStr();
        // 合同存放路径 企业在线签署合同默认存放路径
        String fileFolderPath = uploadPathConfig.getUploadpath() + resultPath;
        File filePath = new File(fileFolderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //合同名称
        String contractDocName = System.currentTimeMillis() + ".pdf";
        File contractDocFile = new File(filePath, contractDocName);
        if (!contractDocFile.exists()) {
            FileUtils.delete(contractDocFile);
            //生成公章
            Map<String, File> seals = sealService.createSeals(dto.getPartyA_companyName(), dto.getPartyB_companyName(), dto.getPartyC_companyName());

            Map<String, String> data = CompanySignContractDto.ofMap(dto);
            data.put("partyAseal", ImgBase64Util.ImgToBase64(seals.get(dto.getPartyA_companyName()).getAbsolutePath()));
            data.put("partyBseal", ImgBase64Util.ImgToBase64(seals.get(dto.getPartyB_companyName()).getAbsolutePath()));
            data.put("partyCseal", ImgBase64Util.ImgToBase64(seals.get(dto.getPartyC_companyName()).getAbsolutePath()));

            try {
                //生成合同
                WordUtil.createHtmlToPdf(data, docConfig.getCompanyOnlineSignContractTemplate(), filePath.getAbsolutePath(), contractDocName);
            } catch (Exception e) {
                log.error("企业:{}在线签署合同生成合同报错：", dto.getPartyA_companyName(), e);
                throw SmartException.of(StatusMsg.StatusMsg_199);
            }
        }
        return resultPath + File.separator + contractDocName;
    }

}
